package com.aquarium.ui.controller;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.Spinner;

import com.aquarium.R;
import com.aquarium.config.ModuleData;
import com.aquarium.config.WaterMotorData;
import com.aquarium.config.WaveMode;
import com.aquarium.config.WaveMotorData;
import com.aquarium.data.ModuleSettingDataBase;
import com.aquarium.module.ModuleControlService;

public class WaveMotorTimePicker extends ModuleTimePicker {

	private final static String TAG = "WaveMotorTimePicker";

	private Spinner mWaveModeSpinner;
	// private String[] mWaveMode;

	private int mPosition = -1;
	private int mWaveMotorIndex = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wave_motor_time_picker);
		init();
	}

	@Override
	protected void subInit() {
		super.subInit();
		System.out.println("XXXXXXXXXXXXXXX");
		if (mIntentAction.equals(ControllerSetting.ACTION_MODIFY_SETTING)) {
			mPosition = getIntent().getIntExtra(
					ControllerSetting.INTENT_EXTRA_LISTVIEW_CLICKED_POSITION,
					-1);
		}
		mWaveMotorIndex = getIntent().getIntExtra(
				WaveMotorSetting.INTENT_EXTRA_WAVE_MOTOR_INDEX, -1);
	}

	@Override
	public void init() {
		super.init();
		setWaveModeSpinnerAdapter();
		if (mPosition != -1 && mWaveMotorIndex != -1) {
			WaveMotorData data = mModuleSettingDataBase
					.queryWaveMotorData(mModuleSettingDataBase
							.getWaveMotorCursor(mWaveMotorIndex, mPosition));
			setWidgetValue(data);
		}
	}

	@Override
	public void setWidgetValue(ModuleData data) {
		super.setWidgetValue(data);
		int seletePos = ((WaveMotorData) data).waveMode - 1;
		mWaveModeSpinner.setSelection(seletePos);
	}

	@Override
	public void findViews() {
		super.findViews();
		mWaveModeSpinner = (Spinner) findViewById(R.id.wave_mode_spinner);
	}

	@Override
	public void setListeners() {
		super.setListeners();
		mOkButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				WaveMotorData data = new WaveMotorData();
				setModuleData(data);
				data.index = mWaveMotorIndex;
				data.waveMode = mWaveModeSpinner.getSelectedItemPosition() + 1;
				if (mIntentAction.equals(ControllerSetting.ACTION_ADD_SETTING)) {
					mModuleSettingDataBase.insertWaveMotorData(data);
				} else if (mIntentAction
						.equals(ControllerSetting.ACTION_MODIFY_SETTING)) {
					mModuleSettingDataBase.updateWaveMotorData(mPosition, data);
				}
				Intent intent = new Intent();
				switch (mWaveMotorIndex) {
				case 1:
					intent = new Intent(
							ModuleControlService.ACTION_UPDATE_WAVE_MOTOR_1_DATA);
					break;
				case 2:
					intent = new Intent(
							ModuleControlService.ACTION_UPDATE_WAVE_MOTOR_2_DATA);
					break;
				default:
					break;
				}

				sendBroadcast(intent);
				finish();
			}
		});
		mCancelButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void setWaveModeSpinnerAdapter() {
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.wave_mode_array,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mWaveModeSpinner.setAdapter(adapter);
	}
}
